package com.eshop.modules.category.service.impl;

import java.lang.invoke.SerializedLambda;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Set;
import com.google.common.collect.Maps;
import java.util.Collection;
import org.springframework.util.CollectionUtils;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.function.Function;
import java.util.LinkedHashSet;
import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.category.service.dto.StoreCategoryDto;
import java.util.LinkedHashMap;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.category.service.dto.StoreCategoryQueryCriteria;
import com.eshop.utils.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.enums.ShopCommonEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eshop.utils.CateDTO;
import java.util.List;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.category.service.StoreCategoryService;
import com.eshop.modules.category.domain.StoreCategory;
import com.eshop.modules.category.service.mapper.StoreCategoryMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class StoreCategoryServiceImpl extends BaseServiceImpl<StoreCategoryMapper, StoreCategory> implements StoreCategoryService
{
    private final IGenerator generator;
    
    public List<CateDTO> getList() {
        final LambdaQueryWrapper<StoreCategory> wrapper = (LambdaQueryWrapper<StoreCategory>)new LambdaQueryWrapper();
        (wrapper.eq(StoreCategory::getIsShow, ShopCommonEnum.SHOW_1.getValue())).orderByAsc(StoreCategory::getSort);
        final List<CateDTO> list = (List<CateDTO>)this.generator.convert(((StoreCategoryMapper)this.baseMapper).selectList((Wrapper)wrapper), (Class)CateDTO.class);
        return (List<CateDTO>)TreeUtil.list2TreeConverter((List)list, 0);
    }
    
    public Map<String, Object> queryAll(final StoreCategoryQueryCriteria criteria, final Pageable pageable) {
        //调用父类(抽象类)方法进行分页处理
        this.getPage(pageable);
        final PageInfo<StoreCategoryDto> page = (PageInfo<StoreCategoryDto>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<StoreCategoryDto> queryAll(final StoreCategoryQueryCriteria criteria) {
        return (List<StoreCategoryDto>)this.generator.convert(((StoreCategoryMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate((Object)StoreCategory.class, (Object)criteria)), (Class)StoreCategoryDto.class);
    }
    
    public void download(final List<StoreCategoryDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final StoreCategoryDto yxStoreCategory : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("父id", yxStoreCategory.getPid());
            map.put("分类名称", yxStoreCategory.getCateName());
            map.put("排序", yxStoreCategory.getSort());
            map.put("图标", yxStoreCategory.getPic());
            map.put("是否推荐", yxStoreCategory.getIsShow());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public Map<String, Object> buildTree(final List<StoreCategoryDto> categoryDTOS) {
        Set<StoreCategoryDto> trees = new LinkedHashSet<StoreCategoryDto>();
        final Set<StoreCategoryDto> cates = new LinkedHashSet<StoreCategoryDto>();
        final List<String> deptNames = categoryDTOS.stream().map(StoreCategoryDto::getCateName).collect(Collectors.toList());
        final List<StoreCategory> categories = (List<StoreCategory>)this.list();
        for (final StoreCategoryDto deptDTO : categoryDTOS) {
            Boolean isChild = false;
            if ("0".equals(deptDTO.getPid().toString())) {
                trees.add(deptDTO);
            }
            for (final StoreCategoryDto it : categoryDTOS) {
                if (it.getPid().equals(deptDTO.getId())) {
                    isChild = true;
                    if (deptDTO.getChildren() == null) {
                        deptDTO.setChildren(new ArrayList<StoreCategoryDto>());
                    }
                    deptDTO.getChildren().add(it);
                }
            }
            if (isChild) {
                cates.add(deptDTO);
            }
            for (final StoreCategory category : categories) {
                if (category.getId().equals(deptDTO.getPid()) && !deptNames.contains(category.getCateName())) {
                    cates.add(deptDTO);
                }
            }
        }
        if (CollectionUtils.isEmpty((Collection)trees)) {
            trees = cates;
        }
        final Integer totalElements = (categoryDTOS != null) ? categoryDTOS.size() : 0;
        final Map<String, Object> map = Maps.newHashMap();
        map.put("totalElements", totalElements);
        map.put("content", CollectionUtils.isEmpty((Collection)trees) ? categoryDTOS : trees);
        return map;
    }
    
    public boolean checkCategory(final int pid) {
        if (pid == 0) {
            return true;
        }
        final StoreCategory storeCategory = (StoreCategory)this.getOne((Wrapper)Wrappers.<StoreCategory>lambdaQuery().eq(StoreCategory::getId, (Object)pid));
        return storeCategory.getPid() <= 0;
    }
    
    public boolean checkProductCategory(final int id) {
        final StoreCategory storeCategory = (StoreCategory)this.getOne((Wrapper)Wrappers.<StoreCategory>lambdaQuery().eq(StoreCategory::getId, (Object)id));
        return storeCategory.getPid() != 0;
    }
    
    public StoreCategoryServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
